$(function(){


    const host = 'http://video.shnongc.cn';
    let imgUrl = '';

	function GetQueryString (name) { //获取指定name的url参数
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return (r[2]);
        return null;
    }
    function Toast (txt){
        var toastHtml = '<div class="tk_info" id="tkInfo"></div>';
        var toast = $('body #tkInfo');
        if (toast.length) {
            toast.html(txt).show();
        }else{
            $('body').append(toastHtml);
            $('body #tkInfo').html(txt).show();
        }
        setTimeout(function(){
          $('body #tkInfo').hide();
        },2000)
    }


    const userId = GetQueryString('userId')||'9381331';
    const token = GetQueryString('token')||'8c6429d4292647a8b7d7d3c457bb4647';
    const code = GetQueryString('code')||'123456';


    $('#yqNum').html(code)
    $('#copyBtn').attr('data-clipboard-text',code)
    //复制邀请码
    var clipboard1 = new ClipboardJS('#copyBtn');
    clipboard1.on('success', function(e){
        Toast('复制成功！')
    });
    clipboard1.on('error', function(e){
        Toast('复制失败！')
    });

    //复制连接
    var clipboard2 = new ClipboardJS('#copyUrl');
    clipboard2.on('success', function(e){
        Toast('复制成功！')
    });
    clipboard2.on('error', function(e){
        Toast('复制失败！')
    });

    //规则说明
    $('.rule').on('click',function(){
        $('.tkbox-bg').show()
    })
    //关闭弹框
    $('.close').on('click',function(){
        $('.tkbox-bg').hide()
    })
    //一键邀请
    $('.invite-btn').on('click',function(){
        //打微信，发送邀请码页面
        console.log('一键邀请')
        window.webkit.messageHandlers.MJShare.postMessage({platformType:2,url:'http://xqt.shnongc.cn/Vtask/red.html'})  

    })
    //我的收益
    $('.icon-hanld').on('click',function(){
        if ($(this).hasClass('animate')) {
            $(this).removeClass('animate');
            $('.award-detail').slideUp();
        }else{
             $(this).addClass('animate');
            $('.award-detail').slideDown();
        }
    })

    //文案滚动
    function txtScroll(obj){
        const len = obj.find('li').length;
        let i = -1;
        let timer = null;
        timer = setInterval(function(){
            if (i==len-1) {
                i=-1;
                obj.css('marginTop','0')
            }else{
                i++;
                obj.animate({'marginTop':-i*0.8+'rem'},200)
            }
        },1000)
    };
    txtScroll($('.scroll-txt ul'))

    //初使化
    function info() {
        $.ajax({
            type:'get',
            dataType: 'json',
            url:host+'/system/api/transfer/inviteAwardList',
            data: {
                'userId':userId,
                'token':token
            },
            //请求成功时执行
            success: function(v) {
                const code = v.code;
                const msg = v.msg;
                const data = v.data;
                if (code == 0) {
                    if (data&&data.length>0) {
                        $('#list').show();
                        $('#myAwards').hide();
                        data.forEach(function(item,index){
                            let btnHtml = '--';
                            if (item.isSee != 1) {
                                btnHtml ='<a href="javascript:;" class="call-btn" onClick="callFun">提醒TA</a>'
                            }
                            let html = '<li><span class="name">'+item.userName+'</span>'+
                                        '<span class="red">'+item.money+'元</span>'+
                                        '<span class="red">'+item.totalMoney+'元</span>'+
                                        '<span>'+btnHtml+'</span></li>';
                            $('#list ul').append(html);
                        })
                    }
                }else{
                    Toast(msg);
                }
            },
            //请求失败遇到异常触发
            error: function(d) {
                Toast(d.msg);
            }
        })
    }
    info();


    //生成二维码
    var qrcode = new QRCode(document.getElementById("qrcode"),{
        width : 200,
        height : 200
    });
    function makeCode () {      
        var elText = document.getElementById("text");
        if (!elText.value) {
            Toast("Input a text");
            elText.focus();
            return;
        }
        qrcode.makeCode(elText.value);
    }
    $('#ewmBtn').on('click',function(){
        makeCode();
        $('.qrcode').show();
    })
    //二维码隐藏
    $('.qrcode').on('click',function(e){
        var t = $(e.target);
        if (t.attr('class') == 'qrcode') {
            $(this).hide();
            return;
        }
        
    })
    //微信邀请
    $('#openWX').on('click',function(){
        console.log('微信邀请');
        window.webkit.messageHandlers.MJShare.postMessage({platformType:2,url:'http://xqt.shnongc.cn/Vtask/red.html'})  
    })
    //朋友圈
    $('#openPYQ').on('click',function(){
        console.log('朋友圈');
        window.webkit.messageHandlers.MJShare.postMessage({platformType:1,url:'http://xqt.shnongc.cn/Vtask/red.html'})  
    })
    //QQ邀请
    $('#openQQ').on('click',function(){
        console.log('QQ邀请');
        window.webkit.messageHandlers.MJShare.postMessage({platformType:3,url:'http://xqt.shnongc.cn/Vtask/red.html'})  
    })

    function callFun(){
        console.log('微信邀请');
        window.webkit.messageHandlers.MJShare.postMessage({platformType:2,url:'http://xqt.shnongc.cn/Vtask/red.html'})  
    }
})